package com.doodod.api.controller.base;

import com.doodod.api.exception.ApiException;
import com.doodod.api.model.base.ErrorCode;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 邱孝兵
 * Date: 14-3-13
 * Time: 下午1:58
 */
public abstract class BaseController extends ActionSupport{

    private static final long serialVersionUID = 1L;

    @ExceptionHandler(ApiException.class)
    @ResponseStatus(value = HttpStatus.OK)
    @ResponseBody
    public ErrorCode handleResourceNotFoundException(ApiException ex)
    {
        return ex.getErrorCode();
    }

    /**
     * 判断当前用户是否正常登陆
     *
     * @return
     */
    public boolean isNormalLogin() {
        ActionContext actionCtx = ActionContext.getContext();
        Map<String, Object> session = actionCtx.getSession();
        if (session != null && session.containsKey("userEmail") && session.containsKey("token")) {
            String userEmail = session.get("userEmail").toString();
            String token = session.get("token").toString();
            if (null != userEmail && !"".equals(userEmail) && null != token && !"".equals(token))
                return true;
        }
        return false;
    }
}
